#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
using namespace std;

class Solution {
    int dx[8] = { 0,0,1,-1,1,1,-1,-1 };
    int dy[8] = { 1,-1,0,0,1,-1,1,-1 };
    int m;
    int n;
public:
    void dfs(vector<vector<char>>& board, int i, int j)
    {
        if (board[i][j] == 'M')
        {
            board[i][j] = 'X';
            return;
        }
        int mine = 0;
        for (int k = 0; k < 8; ++k)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M') ++mine;
        }
        if (mine == 0) board[i][j] = 'B';
        else
        {
            board[i][j] = mine + '0';
            return;
        }
        for (int k = 0; k < 8; ++k)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E')
                dfs(board, x, y);
        }
    }
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        m = board.size();
        n = board[0].size();
        dfs(board, click[0], click[1]);
        return board;
    }
};